<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      height: 0;
    }

    .box {
      border: 1px solid black;
      width: 600px;
      height: 285px;
      background-color: skyblue;
      margin: 100px auto;
      position: relative;
    }

    h2 {
      text-align: center;
    }

    .box1 {
      width: 600px;
      margin: 80px auto;
      display: flex;
      font-size: 25px;
      line-height: 40px;
      left: 210px;
      position: absolute;
    }

    .box2 {
      width: 600px;
      margin: 100px auto;
      display: flex;
      font-size: 25px;
      line-height: 40px;
      left: 100px;
      top: 50px;
      position: absolute;
    }

    .qs,
    .ps {
      width: 450px;
      height: 40px;
      color: red;
    }

    .btns {
      position: absolute;
      bottom: 35px;
    }

    .btns button {
      width: 120px;
      height: 35px;
      margin: 0 50px;
      position: relative;
      left: 80px;
    }

    .btns button:hover {
      cursor: pointer;
    }
  </style>
</head>

<body>
  <div class="box">
    <h2>随机点名</h2>
    <div class="box1">
      <span>名字是：</span>
      <div class="qs">这里显示姓名</div>
    </div>
    <div class="box2">
      <span>衰嗨：</span>
      <div class="ps"></div>
    </div>
    <div class="btns">
      <button class="start">开始</button>
      <button class="end">结束</button>
    </div>
  </div>

  <script>
    let n = 0
    let random = 0
    const arr = ['蔡徐坤', '科比', '乐迪', '马嘉祺', '月半猫']
    const start = document.querySelector('.start')
    const end = document.querySelector('.end')
    const qs = document.querySelector('.qs')
    const ps = document.querySelector('.ps')
    start.addEventListener('click', function () {
      n = setInterval(function () {
        random = Math.floor(Math.random() * arr.length)
        qs.innerHTML = arr[random]
        if (arr.length !== 1) {
          ps.innerHTML = ''
        } else {
          ps.innerHTML = `恭喜*${arr[random]}*被抽到，还剩${arr.length - 1}个人`
        }
      }, 35)
      if (arr.length === 1) {
        start.disabled = end.disabled = true
      }
    })
    end.addEventListener('click', function () {
      ps.innerHTML = `恭喜*${arr[random]}*被抽到，还剩${arr.length - 1}个人`
      clearInterval(n)
      arr.splice(random, 1)
    })
  </script>
</body>

</html>